package com.example.hospital.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.hospital.pojo.*;
import com.example.hospital.service.IOperationService;
import com.example.hospital.service.IPatientService;
import com.example.hospital.service.IRoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;
@Api(tags = "OperationController")
@RestController
@RequestMapping("/operation")
public class OperationController {
    @Autowired
    private IOperationService iOperationService;
    @Autowired
    private IPatientService iPatientService;
    @Autowired
    private IRoService iRoService;
    @ApiOperation(value = "分页查询业务")
    @GetMapping("/bypage")
    public ResPageBean getOperationByPage(@RequestParam(defaultValue = "1") Integer currentPage,
                                              @RequestParam(defaultValue = "10")Integer size,@RequestParam(defaultValue = "") String keyword){
        return iOperationService.getOperationByPage(currentPage,size,keyword);
    }

    @ApiOperation(value = "查询业务")
    @GetMapping("/")
    public List<Operation> getOperation(){
        return iOperationService.list();
    }
    @ApiOperation(value = "根据id获取业务信息")
    @GetMapping("/byid")
    public List<Operation> getOperationByid(@RequestParam Integer id){
        return iOperationService.list(new LambdaQueryWrapper<Operation>().eq(Operation::getId,id));
    }
    @ApiOperation(value = "添加业务")
    @PostMapping("/")
    public RespBean addOperation(@RequestBody Operation operation){
        if(iOperationService.save(operation)){
            return RespBean.success("添加成功");
        }else {
            return RespBean.error("添加失败");
        }
    }
    @ApiOperation(value = "修改业务")
    @PutMapping("/")
    public RespBean updateOperation(@RequestBody Operation operation){
        if(iOperationService.updateById(operation)){
            return RespBean.success("修改成功");
        }else {
            return RespBean.error("修改失败");
        }
    }
    @ApiOperation(value = "删除业务")
    @DeleteMapping("/{id}")
    public RespBean deleteOperation(@PathVariable Integer id){
        if(iOperationService.removeById(id)){
            return RespBean.success("删除成功");
        }else {
            return RespBean.error("删除失败");
        }

    }
    @ApiOperation(value = "获取病人所有业务")
    @GetMapping("/getOperation")
    public List<Patient> getOperation(@RequestParam Integer id){
        LambdaQueryWrapper<Ro> lam  = new LambdaQueryWrapper<>();
        //第一个时间
        LocalDate localDate = LocalDate.now();
        lam.eq(Ro::getPid,id);
        List<Ro> roList = iRoService.list(lam);
        for (Ro ro : roList){
            Integer id1 = ro.getId();
            //第二个时间
            LocalDate endDate = ro.getEndDate();
            if(localDate.isAfter(endDate)){
                iRoService.removeById(id1);
            }
        }
        return iPatientService.getOperation(id);
    }
}
